﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPIDesktopClient.Entities.Xml;
using PPIDesktopClient.General;

namespace PPIDesktopClient.Controls.OutboxDatabase
{
    public partial class OutboxDatabase : UserControl
    {
        private string m_SelectedConnectionString;
        public string SelectedConnectionString
        {
            get
            {
                return m_SelectedConnectionString;
            }
            set
            {
                m_SelectedConnectionString = value;
            }
        }

        public OutboxDatabase()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            LoadData(true);
        }

        public void LoadData(bool allDatabase, string openedConnection)
        {
            List<EOutbox> _Databases;
            if (allDatabase)
            {
                _Databases = XMLHelper.GetDatabases(Constants.K_XmlOutboxPath);
                colUploaded.Visible = false;           
            }
            else
            {
                _Databases = XMLHelper.GetDatabasesToUpload(Constants.K_XmlOutboxPath);
                if (openedConnection != null)
                {
                    foreach (EOutbox o in _Databases)
                    {
                        if (o.ConnectionString == openedConnection)
                        {
                            o.Upload = true;
                        }
                    }                
                }
                colUploaded.Visible = true;
            }
                
            grdOutbox.DataSource = _Databases;
        }

        public void LoadData(bool allDatabase)
        {
            LoadData(allDatabase, null);
        }

        public void LoadDatatoSelect()
        {
            LoadData(false, null);
            colUploaded.HeaderText = "Select";
            grdOutbox.MultiSelect = false;
        }

        private void grdOutbox_SelectionChanged(object sender, EventArgs e)
        {
            m_SelectedConnectionString = null;
            if (grdOutbox.SelectedRows.Count > 0)
            {
                m_SelectedConnectionString = ((EOutbox)(grdOutbox.SelectedRows[0].DataBoundItem)).ConnectionString;    
            }
        }

        public bool HasDatabase()
        {
            return (grdOutbox.Rows.Count > 0);
        }

        public List<EOutbox> GetChekeckDatabases()
        {
            List<EOutbox> _databases = new List<EOutbox>();
            EOutbox _CheckedDatabase;
            
            foreach (DataGridViewRow  r in grdOutbox.Rows)
            {
                _CheckedDatabase = (EOutbox)r.DataBoundItem;
                if (_CheckedDatabase.Upload)
                    _databases.Add(_CheckedDatabase);
            }
            return _databases;
        }

        

    }
}
